{
 "cells": [
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "This IPython Notebook contains simple examples of the line function. \n",
    "\n",
    "To clear all previously rendered cell outputs, select from the menu:\n",
    "\n",
    "    Cell -> All Output -> Clear"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import time\n",
    "\n",
    "import numpy as np\n",
    "from bokeh.io import push_notebook, show, output_notebook\n",
    "from bokeh.models import HoverTool\n",
    "from bokeh.plotting import figure \n",
    "output_notebook()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "N = 1000\n",
    "x = np.random.random(size=N) * 100\n",
    "y = np.random.random(size=N) * 100\n",
    "radii = np.random.random(size=N) * 2\n",
    "colors = [\"#%02x%02x%02x\" % (int(r), int(g), 150) for r, g in zip(50+2*x, 30+2*y)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "TOOLS=\"crosshair,pan,wheel_zoom,box_zoom,reset,tap,box_select,lasso_select\"\n",
    "\n",
    "p = figure(tools=TOOLS)\n",
    "p.axis.major_label_text_font_size = \"24px\"\n",
    "hover = HoverTool(tooltips=None, mode=\"vline\")\n",
    "p.add_tools(hover)\n",
    "r = p.circle(x,y, radius=radii, \n",
    "             fill_color=colors, fill_alpha=0.6, line_color=None, \n",
    "             hover_fill_color=\"black\", hover_fill_alpha=0.7, hover_line_color=None)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# get and explicit handle to update the next show cell with\n",
    "target = show(p, notebook_handle=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "i = 0\n",
    "while True:\n",
    "    i +=1 \n",
    "    p.title.text = str(i)\n",
    "    \n",
    "    r.data_source.data['radius'] = radii * (2 + np.sin(i/5))\n",
    "    \n",
    "    x = r.data_source.data['x']\n",
    "    y = r.data_source.data['y']\n",
    "    d = np.sqrt((x-50)**2 + (y-50)**2)/100\n",
    "    rand = 2 * (np.random.random(size=N) - 0.5)\n",
    "    r.data_source.data['x'] = x + 2 * np.sin(d) * rand\n",
    "    r.data_source.data['y'] = y + np.cos(d**2) * rand\n",
    "    \n",
    "    p.axis.major_label_text_color = r.data_source.data['fill_color'][int(i%N)]\n",
    "\n",
    "    # push updates to the plot continuously using the handle (intererrupt the notebook kernel to stop)\n",
    "    push_notebook(handle=target)\n",
    "    time.sleep(0.1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Update the hover glyph propertes using the explicit handle (go hover over the plot)\n",
    "r.hover_glyph.fill_color = \"white\"\n",
    "r.hover_glyph.fill_alpha = 0.5\n",
    "hover.mode = \"vline\"\n",
    "push_notebook()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.4.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
